
**** Table H.13: allowing for industry sipplovers within zipcode
use "$datadir\ewallet\merchants_reshape_week.dta" , clear 

	*** Keep only valid pincodes (6 digits)
	tostring pincode, gen(pincode_str)
	g x = length(pincode_str)
	keep if x == 6 
	drop x pincode_str
	
	unique pincode
	
	** Week 28 is the week of the shock
	g onplatform = transaction > 0
	
	egen merchant_id = group(merchantid)
	egen ziptime = group(pincode time)
	egen zip = group(pincode)
	egen industrytime = group(industryid time)
	egen districttime = group(district state time)
	
	
	bys  ziptime : egen amount_jt = total(amount) 
	bys  ziptime : egen transaction_jt = total(transaction) 
	bys  ziptime : egen onplatform_jt = total(onplatform) 
	
	xtset merchant_id time 
	
	foreach var in amount transaction onplatform {
		g lag_`var'_jt = l.`var'_jt
		g lag_`var' = l.`var'
		g lag_`var'_jt_1 = lag_`var'_jt - lag_`var'
		
		g ln_lag_`var' = ln(1+lag_`var')
		g ln_lag_`var'_jt_1 = ln(1+lag_`var'_jt_1)
		g ln_`var' = ln(1+`var')
	}
	
	foreach var in ln_lag_amount  ln_lag_amount_jt_1 ln_amount ln_lag_transaction  ln_lag_transaction_jt_1 ln_transaction {
	bys time: egen m_`var'=mean(`var')
	bys time: egen sd_`var'=sd(`var')
	gen `var'_z=(`var'-m_`var')/sd_`var'
	drop m_`var' sd_`var'
	sum `var'_z
	}
	rename ln_lag_amount_jt_1_z ln_lag_amount_z_jt_1
	rename ln_lag_transaction_jt_1_z  ln_lag_transaction_z_jt_1

	**** amount
	foreach var in  amount  {
		reg ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_1.tex", dec(3) replace
		reghdfe ln_`var' ln_lag_`var'  c.ln_lag_`var'_jt_1 , a(merchant_id ) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_1.tex", dec(3) append
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id industrytime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_1.tex", dec(3) append
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id industrytime districttime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_1.tex", dec(3) append
	
	}
	
	**** transactions
	foreach var in  transaction {
		reg ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_2.tex", dec(3) replace
		reghdfe ln_`var' ln_lag_`var'  c.ln_lag_`var'_jt_1 , a(merchant_id ) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_2.tex", dec(3) append
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id industrytime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_2.tex", dec(3) append
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id industrytime districttime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_2.tex", dec(3) append
	
	}
	
	**** on platform
	foreach var in onplatform {
		reg `var' lag_`var' c.ln_lag_`var'_jt_1 , cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_3.tex", dec(3) replace
		reghdfe `var' lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id ) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_3.tex", dec(3) append
		reghdfe `var' lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id industrytime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_3.tex", dec(3) append
		reghdfe `var' lag_`var' c.ln_lag_`var'_jt_1, a(merchant_id industrytime districttime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_13_3.tex", dec(3) append
	}
	
